<template>
  <div ref="wrapper" class="wrapper">
      <div>
        <slot></slot>
      </div>
  </div>
</template>

<script type="text/ecmascript-6">
  import BScroll from 'better-scroll'

  export default {
    props: {
      probeType: {
        type: Number,
        default: 1
      },
      click: {
        type: Boolean,
        default: true
      },
      listenScroll: {
        type: Boolean,
        default: false
      },
      data: {
        type: Array,
        default: null
      },
      pullup: {
        type: Boolean,
        default: false
      },
       pulldown: {
            type: Boolean,
            default: false
        },
      beforeScroll: {
        type: Boolean,
        default: false
      },
      refreshDelay: {
        type: Number,
        default: 20
      },
      loadingStatus: {
            type: Object,
            default: function () {
                return {
                    showIcon: false,
                    status: ''
                };
            }
        },
        /**
         * 是否启用下拉刷新的交互
         */
        pulldownUI: {
            type: Boolean,
            default: false
        },
        /**
         * 是否启用上拉加载的交互
         */
        pullupUI: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            loadingConnecting: false,
            pulldownTip: {
                text: '下拉刷新',     // 松开立即刷新
                rotate: ''    // icon-rotate
            },

        };
    },
    mounted() {
      setTimeout(() => {
        this._initScroll()
      }, 20)
    },
    methods: {
      _initScroll() {
        if (!this.$refs.wrapper) {
          return
        }
        this.scroll = new BScroll(this.$refs.wrapper, {
          probeType: this.probeType,
          click: this.click
        })
        // 是否派发滚动事件
        if (this.listenScroll || this.pulldown || this.pullup) {
            let me = this;
            this.scroll.on('scroll', (pos) => {
                if (this.listenScroll) {
                    me.$emit('scroll', pos);
                }

                if (this.pulldown) {
                    // 下拉动作
                    if (pos.y >10) {
                        this.pulldownTip = {
                            text: '松开立即刷新',
                            rotate: 'icon-rotate'
                        }
                    } else {
                        this.pulldownTip = {
                            text: '下拉刷新',     // 松开立即刷新
                            rotate: ''    // icon-rotate
                        }
                    }
                }

                if (this.pullup) {

                }
            })
        }

        // 是否派发滚动到底部事件，用于上拉加载
         if (this.pullup) {
            this.scroll.on('scrollEnd', () => {
                // 滚动到底部
                if (this.scroll.y <= (this.scroll.maxScrollY + 50)) {
                    this.$emit('pullup');
                    this.$emit('scrollToEnd');
                }
            });
        }

        // 是否派发顶部下拉事件，用于下拉刷新
        if (this.pulldown) {
            this.scroll.on('touchend', (pos) => {
                // 下拉动作
                if (pos.y > 50) {
                    setTimeout(() => {
                        // 重置提示信息
                        this.pulldownTip = {
                            text: '下拉刷新',     // 松开立即刷新
                            rotate: ''    // icon-rotate
                        }
                    },600);
                    this.$emit('pulldown');
                }
            });
        }
        if (this.listenScroll) {
          let me = this
          this.scroll.on('scroll', (pos) => {
            me.$emit('scroll', pos)
          })
        }

        if (this.pullup) {
          this.scroll.on('scrollEnd', () => {
            if (this.scroll.y <= (this.scroll.maxScrollY + 50)) {
              this.$emit('scrollToEnd')
            }
          })
        }
        //派发下拉事件，用于下拉刷新
        if (this.pulldown) {
          this.scroll.on('touchend', (pos) => {
            // 下拉动作
            if (pos.y > 50) {
              this.$emit('pulldown')
            }
          })
        }
        if (this.beforeScroll) {
          this.scroll.on('beforeScrollStart', () => {
            this.$emit('beforeScroll')
          })
        }
      },
      disable() {
        this.scroll && this.scroll.disable()
      },
      enable() {
        this.scroll && this.scroll.enable()
      },
      refresh() {
        this.scroll && this.scroll.refresh()
      },
      scrollTo() {
        this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
      },
      scrollToElement() {
        this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
      }
    },
    watch: {
      data() {
        setTimeout(() => {
          this.refresh()
        }, this.refreshDelay)
      }
    }
  }
</script>

<style type="text/css" lang="scss" scoped="">
    .loading-connecting{
        position:absolute;
        top:0;
        width:100%;
        background:rgba(0,0,0,0.5);
        text-align:center;
        padding-top:20px;
        color:#fff;
        z-index:9999;
    }
</style>
